#include "count.h"
#include "ui_count.h"
#include <QPainter>
#include <QPen>
#include <QPixmap>
#include <random>

Count::Count(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Count)
{
    ui->setupUi(this);

    select = 0;
    temp = false;
    humi = false;

    ui->m_label_text->setFixedSize(100,50);
    ui->m_label->setFixedSize(750,300);

    timer = new QTimer(this);

    connect(timer,&QTimer::timeout,this,&Count::drawLineChart);
    timer->start(1000);

    timer1 = new QTimer(this);
    connect(timer,&QTimer::timeout,this,&Count::updatetimeSlots);
    timer1->start(1000);

}

Count::~Count()
{
    delete ui;
    m_points_humi.clear();
    time.clear();
    m_points_temp.clear();

}

void Count::drawLineChart(/*QLabel *label,QVector<QPointF> points*/)
{
    if(temp){//收集温度数据     
        static int number1=0;
        m_points_temp.append(QPointF(number1,m_lcd1));
        number1 += 40;
    }
    if(humi){//收集湿度数据
        //m_lcd2 = rand()%100;
        static int number1=0;
        m_points_humi.append(QPointF(number1,m_lcd2));
        number1 += 40;
    }
    if(select == 1)
    {
        updatedrawLineChart(m_points_humi);
    }
    else if(select == 2)
    {
        updatedrawLineChart(m_points_temp);
    }
}

QPointF Count::mapPointToPlotArea(const QPointF &point, const QRectF &plotArea)
{
    qreal maxX = 800;
    qreal minX = 0;
    qreal maxY = 100;
    qreal minY = 0;

    qreal xRange = maxX - minX;
    qreal yRange = maxY - minY;
    qreal mappedX = plotArea.left()+((point.x()-minX)/xRange)*plotArea.width();
    qreal mappedY = plotArea.bottom()-((point.y()-minY)/yRange)*plotArea.height();
    return QPointF(mappedX,mappedY);
}

void Count::drawAxes(QPainter &painter, QRectF &plotArea)
{

    qreal maxX = 800;
    qreal minX = 0;
    qreal maxY = 100;
    qreal minY = 0;


    // 绘制X轴和Y轴
    painter.drawLine(plotArea.bottomLeft(), plotArea.bottomRight());
    painter.drawLine(plotArea.topLeft(), plotArea.bottomLeft());
    QPen pen;
    pen.setColor(Qt::gray);
    painter.setPen(pen);

    // 绘制X轴刻度
    int numXTicks = 10; // X轴上的刻度数量
    for (int i = 0; i <= numXTicks; ++i) {
        double xTick = minX + (i * (maxX - minX) / numXTicks);
        double x = plotArea.left() + ((xTick - minX) / (maxX - minX)) * plotArea.width();
        painter.drawLine(QPointF(x, plotArea.bottom()), QPointF(x, plotArea.bottom() - 5));
        if(i<numXTicks)
        {
            painter.drawLine(QPointF(x,plotArea.bottom()),QPointF(x,plotArea.top()));
        }
        QPointF point(x-15,plotArea.bottom()+10);
        painter.drawText(point,/*QString::number(xTick)*/time.at(i));
    }

    // 绘制Y轴刻度
    int numYTicks = 10; // Y轴上的刻度数量
    for (int i = 0; i <= numYTicks; ++i) {
        double yTick = minY + (i * (maxY - minY) / numYTicks);
        double y = plotArea.bottom() - ((yTick - minY) / (maxY - minY)) * plotArea.height();
        painter.drawLine(QPointF(plotArea.left(), y), QPointF(plotArea.left() + 5, y));
        if(i<numYTicks)
        {
            painter.drawLine(QPointF(plotArea.left(),y),QPointF(plotArea.right(),y));
        }
        painter.drawText(QPointF(plotArea.left() + 10, y + 5), QString::number(yTick));
    }
}

void Count::dataUpSlots2(const QByteArray &msg)
{
    QJsonDocument doc = QJsonDocument::fromJson(msg);

    if(!doc.isNull())
    {
        QJsonObject root = doc.object();
        double lcd1 = root["temp"].toDouble();
        m_lcd1 = lcd1;
        int lcd2 = root["humi"].toInt();
        m_lcd2 = lcd2;
    }
}

void Count::updatedrawLineChart(QVector<QPointF> points)
{
    if(points.size()>2)
    {
        QPixmap pixmap(ui->m_label->size());

        pixmap.fill(Qt::white);

        QPainter painter(&pixmap);
        painter.setRenderHint(QPainter::Antialiasing);

        //设置坐标轴
        QRectF plotArea(5,5,pixmap.width()-30,pixmap.height()-30);

        drawAxes(painter,plotArea);

        QPen pen;
        pen.setWidth(2);
        pen.setColor(Qt::blue);
        painter.setPen(pen);

        QPainterPath path;
        bool first = true;
        for(const QPointF &point : points)
        {
            QPointF mappedPoint = mapPointToPlotArea(point,plotArea);
            if(first)
            {
                path.moveTo(mappedPoint);
                first = false;
            }
            else
            {
                path.lineTo(mappedPoint);
            }
        }
        painter.drawPath(path);
        ui->m_label->setPixmap(pixmap);
        ui->m_label->setAlignment(Qt::AlignLeft | Qt::AlignTop);
        update();
    }
}

void Count::updatetimeSlots()
{
    QDateTime timeCur = QDateTime::currentDateTime();
    QString timeStr = timeCur.toString("hh:mm:ss");

    if(time.size() == 10)
    {
        qDebug()<<"ok";
    }
    if(time.size()<=15)
    {
        time.append(timeStr);
    }else
    {
        time.removeFirst();
        if(select == 1)
        {
            for(QPointF &point : m_points_humi)
            {
                point.setX(point.x()-40);
            }
        }
        if(select == 2)
        {
            for(QPointF &point : m_points_temp)
            {
                point.setX(point.x()-40);
            }
        }
    }
}

void Count::on_m_button_temp_clicked()
{
    ui->m_label_text->setText("温度");
    select = 2;
    temp = true;
}

void Count::on_m_button_humi_clicked()
{
    ui->m_label_text->setText("湿度");
    select = 1;
    humi = true;
}
